
package com.zhu.storesmanager.network;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;

import com.zhu.storesmanager.utils.Log;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;

/**
 * HTTP connection helper 网络请求
 */
public class HttpConnectionMethod extends Thread {
    private static final int TIMEOUT = 20 * 1000; //超时
    public static final int STATUS_ERROR = 0; //状态结果 失败
    public static final int STATUS_SUCCEED = 1; //状态 成功

    //网络请求方式
    public static final int GET = 0;
    public static final int POST = 1;
    public static final int PUT = 2;
    public static final int DELETE = 3;
    public static final int BITMAP = 4;

    private HttpClient httpClient;

    private String url; //url
    private int method;

    private NetworkCallback mNetworkCallback = null; //结果返回
    private String data; //post 数据

    public HttpConnectionMethod(NetworkCallback networkCallback) {
        mNetworkCallback = networkCallback;
    }

    private void create(int method, String url, String data) {
        this.method = method;
        this.url = url;
        this.data = data;
        start();
    }

    public void callGet(String url) {
        create(GET, url, null);
    }

    public void callPost(String url, String data) {
        create(POST, url, data);
    }

    public void callPut(String url, String data) {
        create(PUT, url, data);
    }

    public void callDelete(String url) {
        create(DELETE, url, null);
    }

    public void callBitmap(String url) {
        create(BITMAP, url, null);
    }

    public void run() {
        httpClient = new DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), TIMEOUT);
        try {
            HttpResponse response = null;
            switch (method) {
                case GET:
                    response = httpClient.execute(new HttpGet(url));
                    break;
                case POST:
                    HttpPost httpPost = new HttpPost(url);
                    httpPost.setHeader("Content-Type", "application/json; charset=utf-8");
                    httpPost.setEntity( new StringEntity(data, "utf-8"));
                    response = httpClient.execute(httpPost);
                    break;
                case PUT:
                    HttpPut httpPut = new HttpPut(url);
                    httpPut.setEntity( new StringEntity(data, "utf-8"));
                    response = httpClient.execute(httpPut);
                    break;
                case DELETE:
                    response = httpClient.execute(new HttpDelete(url));
                    break;
                case BITMAP:
                    response = httpClient.execute(new HttpGet(url));
                    break;
            }
            Object object = null;
            if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
            	if (method != BITMAP){
                    object = processEntity(response.getEntity());
                }else{
                    object = processBitmapEntity(response.getEntity());
                }
            	mNetworkCallback.callFinished(STATUS_SUCCEED, object);
            }else{
            	 Log.e("response StatusCode="+response.getStatusLine().getStatusCode());
            	mNetworkCallback.callFinished(STATUS_ERROR, null);
            }
        } catch (Exception e) {
        	e.printStackTrace();
            mNetworkCallback.callFinished(STATUS_ERROR, null);
        }
    }

    private String processEntity(HttpEntity entity) throws IllegalStateException,
            IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(entity
                .getContent()));
        String line, result = "";
        while ((line = br.readLine()) != null)
            result += line;
        return result;
    }

    private Bitmap processBitmapEntity(HttpEntity entity) throws IOException {
        BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
        Bitmap bm = BitmapFactory.decodeStream(bufHttpEntity.getContent());
        return bm;
    }
}
